﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Common;
using DadayunApi.Model;
using Newtonsoft.Json;
using WFFilter = DadayunApi.FormFilter;

namespace DadayunApi.DAL
{
    public class WorkFlowApi : Api
    {
       
        public WorkFlowApi(AppAuthBase authBase, string baseUri) : base(authBase, baseUri)
        {
        }
        public List<WorkFlowTemplateInfo> GetWorkFlows(out int totalCount,int start=0,int limit=0,string sort= "-CreateTime",IEnumerable<WFFilter>filters=null)
        {
            List<WorkFlowTemplateInfo> obj = null;
            var uri = "v1/workflow/templates?start=" + start + "&count=true&sort=" + sort;
            if(filters!=null)
            {
                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                };
               string sfilter = JsonConvert.SerializeObject(filters, settings);
                uri += "&filter=" + sfilter;
            }
            totalCount = GetTotalCount(uri);
            if (limit == 0)
            {
                limit = totalCount;
            }
            uri += "&limit=" + limit;
            FullUri = uri;
            httpStatus = HttpClientHelper.HttpClientGetSync(baseUri, uri, out responseJson, out responseHeader, httpgetTokenHeader);
            totalCount = 0;
            if (httpStatus == HttpStatusCode.OK)
            {
                obj = JsonConvert.DeserializeObject<List<WorkFlowTemplateInfo>>(responseJson);
                
            }
            return obj;
        }
        /// <summary>
        /// 获取工作流模板和实例
        /// 当instaceId不为空时返回模板和实例，否则，返回模板
        /// </summary>       
        /// <returns></returns>
        public WorkFlowTemplateInfo GetWorkFlowTemplateAndInstance(string id,string instanceId = null)
        {

            var uri = "v1/workflow/templates/$" + id;
            if (!string.IsNullOrEmpty(instanceId))
                uri += "?instanceId" + instanceId;
            
           
            WorkFlowTemplateInfo obj = null;
            httpStatus = HttpGet(uri);
            
            if (httpStatus == HttpStatusCode.OK)
            {
                obj = JsonConvert.DeserializeObject< WorkFlowTemplateInfo > (responseJson);
               
            }
            return obj;
        }

        public List< WorkFlowInfo>GetWorkFlows(string id,out int totalCount, int start=0,int limit=0,string sort= "-CreateTime", IEnumerable<WFFilter> filter=null )
        {
            var uri = "v1/workflow/templates/$" + id + "/instances?sort=" + sort;
            if (filter!=null)
            {
                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                };
               string sfilter = JsonConvert.SerializeObject(filter, settings);
                uri += "&filter=" + sfilter;
            }
            totalCount = GetTotalCount(uri);
            if (limit == 0) limit = totalCount;
            uri += "&start=" + start + "&limit=" + limit;
            List< WorkFlowInfo> obj = null;
            httpStatus = HttpGet(uri);

            if (httpStatus == HttpStatusCode.OK)
            {
                obj = JsonConvert.DeserializeObject<List<WorkFlowInfo>>(responseJson);

            }
            return obj;
        }
    }
}
